package com.hydra.auth.repository;

import com.hydra.auth.entity.DictData;
import com.hydra.auth.entity.SysOperateLog;
import com.hydra.auth.mapper.DictDataMapper;
import com.hydra.auth.mapper.SysOperateLogMapper;
import com.hydra.base.operate.log.annotation.OperateLogInject;
import com.hydra.base.operate.log.entity.BaseOperateLogEntity;
import com.hydra.base.services.BaseService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class DictDataService extends BaseService<DictData, DictDataMapper> {

    @Resource
    private SysOperateLogMapper sysOperateLogMapper;

    /**切面进行操作日志记录*/
    @Override
    @OperateLogInject(cate = "SYS_DICT_DATA")
    public void handleOperateLog(DictData data, DictData history) {
        super.handleOperateLog(data, history);
    }

    /**获取操作日志的编码*/
    @Override
    public String getOperateLogCode(DictData data) {
        return data.getTypeCode() + "_" + data.getCode();
    }

    /**保存操作日志*/
    @Override
    public void saveOperateLog(BaseOperateLogEntity operateLogInfo, DictData updateData, DictData historyData) {
        SysOperateLog log = operateLogInfo.copy(SysOperateLog.class);
        sysOperateLogMapper.insert(log);
    }

    /**
     * 根据字典类型编码获取字典数据
     * @param typeCode  字典类型编码
     */
    public List<DictData> getDictDataByTypeCode(String typeCode){
        return this.baseMapper.getDictDataByTypeCode(typeCode);
    }

    /**
     * 根据字典类型、编码查询字典
     * @param typeCode  字典类型
     * @param code      字典编码
     */
    public DictData getDictDataByTypeCodeAndCode(String typeCode, String code){
        return this.baseMapper.getDictDataByTypeCodeAndCode(typeCode, code);
    }

    /**
     * 根据类型编码和状态获取字典数据
     */
    public List<DictData> getDictDataByTypeCodeAndStatus(String typeCode, String status){
        return this.baseMapper.getDictDataByTypeCodeAndStatus(typeCode, status);
    }

    /**
     * 根据字典类型、数据name查询字典
     * @param typeCode  字典类型
     * @param name      数据name
     */
    public DictData getDictDataByTypeCodeAndName(String typeCode, String name){
        return this.baseMapper.getDictDataByTypeCodeAndName(typeCode, name);
    }

}
